package vn.com.shoppie.setting;

import vn.com.shoppie.activity.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class SettingPreference extends PreferenceActivity implements OnSharedPreferenceChangeListener {
	public static boolean PRINT_STACK_TRACE=true;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.pref);
		PreferenceManager.setDefaultValues(this, R.xml.pref, false);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {

	}

	// example
	public static final boolean KEY_SHOW_DESCRIPTION_DEF = true;
	public static final String KEY_SHOW_DESCRIPTION = "show_description";

	public static boolean getShowDescription(Context context) {
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
		return sharedPref.getBoolean(SettingPreference.KEY_SHOW_DESCRIPTION, KEY_SHOW_DESCRIPTION_DEF);
	}

	public static void setShowDescription(Context context, boolean value) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = prefs.edit();
		editor.putBoolean(SettingPreference.KEY_SHOW_DESCRIPTION, value);
		editor.commit();
	}

	// user-id
	public static final int VALUE_USER_ID_DEF = -1;
	public static final String KEY_USER_ID = "user-id";

	public static int getUserID(Context context) {
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
		return sharedPref.getInt(KEY_USER_ID, VALUE_USER_ID_DEF);
	}

	public static void setUserID(Context context, int value) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = prefs.edit();
		editor.putInt(SettingPreference.KEY_USER_ID, value);
		editor.commit();
	}

	// user-deviceToken
	public static final String VALUE_USER_DEVICE_TOKEN_DEF = "";
	public static final String KEY_USER_DEVICE_TOKEN_ID = "user-device-token";

	public static String getUserDeviceToken(Context context) {
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
		return sharedPref.getString(KEY_USER_DEVICE_TOKEN_ID, VALUE_USER_DEVICE_TOKEN_DEF);
	}

	public static void setUserDeviceToken(Context context, String value) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = prefs.edit();
		editor.putString(SettingPreference.KEY_USER_DEVICE_TOKEN_ID, value);
		editor.commit();
	}

	// user-custom code
	public static final String VALUE_USER_CODE_DEF = "";
	public static final String KEY_USER_CODE_ID = "user-code";

	public static String getUserCode(Context context) {
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
		return sharedPref.getString(KEY_USER_CODE_ID, VALUE_USER_CODE_DEF);
	}

	public static void setUserCode(Context context, String value) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = prefs.edit();
		editor.putString(SettingPreference.KEY_USER_CODE_ID, value);
		editor.commit();
	}
	
	// first-use
	public static final boolean VALUE_FIRST_USE_DEF = true;
	public static final String KEY_FIST_USE = "first-use";

	public static boolean getFirstUse(Context context) {
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
		return sharedPref.getBoolean(KEY_FIST_USE, VALUE_FIRST_USE_DEF);
	}

	public static void setFirstUse(Context context, boolean value) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		Editor editor = prefs.edit();
		editor.putBoolean(SettingPreference.KEY_FIST_USE, value);
		editor.commit();
	}
}
